<template>
  <div class="manage">
    <el-dialog
      :title="operateType === 'add' ? '新增用户' : '更新用户'"
      :visible.sync="isShow"
    >
      <common-form
        :formLabel="operatateFormLabel"
        :form="operateForm"
        :inline="true"
        ref="form"
      >
      </common-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isShow = false">取消</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>
      </div>
    </el-dialog>
    <div class="manage-header">
      <el-button type="primary" @click="addUser">+ 新增</el-button>
      <common-form
        :formLabel="formLabel"
        :form="searchForm"
        :inline="true"
        ref="form"
      >
        <el-button type="primary" @click="getList(searchForm.keyword)"
          >搜索</el-button
        >
      </common-form>
    </div>
    <common-table
      :tableData="tableData"
      :tableLabel="tableLabel"
      @changePage="getList()"
      @edit="editUser"
      @delete="deleteUser"
    >
    </common-table>
  </div>
</template>

<script>
import CommonForm from "../../src/components/CommonForm";
import CommonTable from "../../src/components/CommonTable.vue";
import { getUser } from "../../api/data";
export default {
  name: "user",
  components: { CommonForm, CommonTable },
  data() {
    return {
      operateType: "add",
      isShow: false,
      operatateFormLabel: [
        //常规配置数据
        {
          model: "name",
          label: "姓名",
          type: "input",
        },
        {
          model: "age",
          label: "年龄",
          type: "input",
        },
        {
          model: "sex",
          label: "性别",
          type: "select",
          opts: [
            {
              label: "男",
              value: 1,
            },
            {
              label: "女",
              value: 0,
            },
          ],
        },
        {
          model: "phone",
          label: "电话号码",
          type: "input",
        },
        {
          model: "addr",
          label: "地址",
          type: "input",
        },
      ],
      operateForm: {
        //当数据发生变换的时候双向绑定检测数据改变并且赋值
        name: "",
        addr: "",
        age: "",
        phone: "",
        sex: "",
      },
      formLabel: [
        {
          model: "keyword",
          label: "",
          type: "input",
        },
      ],
      searchForm: {
        keyword: "",
      },
      tableData: [
        //表格数据
      ],
      tableLabel: [
        //列数据
        {
          prop: "name", //字段名称
          label: "姓名", //列名
        },
        {
          prop: "age",
          label: "年龄",
        },
        {
          prop: "sexLabel",
          label: "性别",
        },
        {
          prop: "phone",
          label: "电话号码",
          width: 200,
        },
        {
          prop: "addr",
          label: "地址",
          width: 320,
        },
      ],
      config: {
        total: 15,
      },
    };
  },
  methods: {
    confirm() {
      //点击确认时
      if (this.operateType === "edit") {
        //编辑状态
        //定义接口传入数据
        this.$http.post("/user/edit", this.operateForm).then((response) => {
          console.log(response, "请求成功");
          this.isShow = false; //返回数据后关闭弹窗
          this.getList();
        });
      } else {
        //新增状态
        this.$http.post("/user/add", this.operateForm).then((response) => {
          console.log(response, "请求成功");
          this.isShow = false; //返回数据后关闭弹窗
          this.getList();
        });
      }
    },
    addUser() {
      this.isShow = true; //控制弹窗
      this.operateType = "add"; //默认新增
      this.operateForm = {
        //初始化数据
        name: "",
        addr: "",
        age: "",
        phone: "",
        sex: "",
      };
    },
    editUser(row) {
      this.operateType = "edit"; //置为编辑字段
      this.isShow = true;
      this.operateForm = row;
    },
    deleteUser(row) {
      //通知栏内容
      this.$confirm("此操作将永久删除该数据，是否继续？", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        const id = row.id;
        this.$http
          .get("/user/delete", {
            params: { id },
          })
          .then(() => {
            this.$message({
              type: "success",
              message: "删除成功",
            });
            this.getList();
          });
      });
    },
    getList(name = "") {
      //函数调用
      this.config.loading = true; //给配置加一个loading属性用于获取数据
      name ? (this.config.total = 15) : ""; //判断name是否存在以决定搜不搜索
      //接口调用
      getUser({ name }).then(({ data: response }) => {
        //用then拿回调操作渲染数据
        this.tableData = response.list.map((item) => {
          item.sexLabel = item.sex === 0 ? "女" : "男";
          return item;
        });
        this.config.total = response.count;
        this.config.loading = false;
      });
    },
  },
  created() {
    this.getList();
  },
};
</script>

<style scoped>
.manage-header {
  display: flex;
  margin-top: 20px;
  justify-content: space-between; /*左右靠边*/
  align-items: center; /**上下对齐 */
}
</style>
